export const RawUserMenuParser = (rawUserMenuData) => {
  const result = [];
  for (let item of rawUserMenuData) {
    if (item.page_type === "component") {
      const obj = {
        path: "/" + item.component,
        route_name: item.component + "Router",
        component: compImporter(item.component),
        component_name: item.component + "Comp",
        parent_code: item.parent_code,
        menu_code: item.menu_code,
        need_by_router: true,
        need_by_usermenu: true,
        need_by_topmenu: true,
        page_type: item.page_type,
        meta: {
          hidden: item.hidden,
          keepAlive: item.keep_alive,
          title: item.title,
          icon: item.icon,
        },
      };
      result.push(obj);
    } else if (item.page_type === "sql" && item.sql) {
      // console.log("OKKKKKKKK");
      // console.log(item, "item");
      // sql类型的路由
      const obj = {
        path: "/" + item.sql.sql_type + "/" + item.menu_code,
        route_name: item.sql.sql_type + "_" + item.sql.sql_code + "Router",
        component: reportImporter(item.sql.sql_type),
        component_name: item.sql.sql_type + "Comp",
        parent_code: item.parent_code,
        menu_code: item.menu_code,
        need_by_router: true,
        need_by_usermenu: true,
        need_by_topmenu: true,
        page_type: item.page_type,
        meta: {
          hidden: item.hidden,
          keepAlive: item.keep_alive,
          title: item.title,
          icon: item.icon,
          sql_code: item.sql.sql_code,
          sql_type: item.sql.sql_type,
          // document: item.document,
        },
      };
      result.push(obj);
    } else if (item.page_type === "redirect" && item.redirect) {
      const obj = {
        path: "/redirect/" + item.menu_code,
        route_name: "Redirect" + item.menu_code + "Router",
        component: () => import("pages/Redirect/RedirectIndex.vue"),
        component_name: "redirectComp",
        parent_code: item.parent_code,
        menu_code: item.menu_code,
        need_by_router: true,
        need_by_usermenu: true,
        need_by_topmenu: true,
        page_type: item.page_type,
        meta: {
          hidden: item.hidden,
          keepAlive: item.keep_alive,
          title: item.title,
          icon: item.icon,
          redirect: item.redirect,
        },
      };
      result.push(obj);
    } else if (item.page_type === "directory") {
      const obj = {
        path: null,
        route_name: item.menu_code + "Directory",
        component: null,
        parent_code: item.parent_code,
        menu_code: item.menu_code,
        page_type: item.page_type,
        need_by_router: false,
        need_by_usermenu: false,
        need_by_topmenu: true,
        meta: {
          hidden: item.hidden,
          keepAlive: item.keep_alive,
          title: item.title,
          icon: item.icon,
        },
      };
      // console.log("ITEM测试");
      // console.log(item);
      result.push(obj);
    } else if (item.page_type === "document" ) {

      // 跳转到文档
      const obj = {
        path: "/document/" + item.menu_code,
        route_name: item.menu_code + "Document",
        component: () => import("pages/Document/PreDefineDoc.vue"),
        component_name: item.document + "Doc",
        // document: item.document,
        parent_code: item.parent_code,
        menu_code: item.menu_code,
        need_by_router: true,
        need_by_usermenu: true,
        need_by_topmenu: true,
        page_type: item.page_type,
        meta: {
          hidden: item.hidden,
          keepAlive: item.keep_alive,
          title: item.title,
          icon: item.icon,
          sql_code: "test_day_id_sql",
          redirect: item.redirect,
          document: item.document,
        },
      };
      result.push(obj);
    }
  }
  result.push({
    path: "/:catchAll(.*)*",
    name: "notFound",
    need_by_router: true,
    need_by_usermenu: false,
    need_by_topmenu: false,
    component: () => import("pages/ErrorNotFound.vue"),
  });
  return result;
};

const compImporter = (component) => {
  // Quasar2 版本:
  // console.log(component);
  return () =>
    Promise.resolve(
      require(`src/pages/System/${component}/${component}Comp.vue`).default
    );
  // For Vite https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations
  //return () => Promise.resolve(import(`../${component}.vue`));
  // Quasar1 版本:
  // return (resolve) => require([`src/pages/${component}`], resolve);
};

const reportImporter = (sql_type) => {
  return () => {
    if (sql_type === "query") {
      return import(`src/pages/Report/QueryTemplate/QueryTemplateIndex.vue`);
    } else {
      return Promise.resolve(
        require(`src/pages/Report/PreDefinedTemplate/${
          sql_type[0].toUpperCase() + sql_type.substr(1)
        }.vue`).default
      );
    }
  };
};
